/**
 * @Title: ApacheHttpClientPost.java
 * @Package com.ccdt.tvsibp.protal.test
 * @Description: TODO
 * Copyright: Copyright (c) 2018 
 * Company:CCDT
 * 
 * @author xierh
 * @date 2014-6-18 上午11:27:24
 * @version V1.0
 */

package com.zpkj.portal.utils;

import java.io.IOException;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

/**
 * Post请求测试类
 */
@SuppressWarnings("all")
public class ApacheHttpClient {

	/**
	 * 功能：(采用Apache 开源 HttpClient 请求获取返回数据)
	 */
	public static String httpPost(String URL, List<NameValuePair> params) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60 * 1000);// 连接时间
		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60 * 1000);// 数据传输时间
		HttpPost postRequest = new HttpPost(URL);
		postRequest.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8));

		HttpResponse response;

		try {
			response = httpClient.execute(postRequest);
			if (response.getStatusLine().getStatusCode() == 200) {
				String result = EntityUtils.toString(response.getEntity(), "UTF-8");
				httpClient.getConnectionManager().shutdown();
				return result.toString();
			} else {
				throw new Exception("请求外部系统出现异常");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ClientConnectionManager conn = httpClient.getConnectionManager();
			if (conn != null) {
				conn.shutdown();
			}
			httpClient = null;
		}
		return "";
	}

	/**
	 * 执行一个HTTP GET请求，返回请求响应的HTML
	 */
	public static String httpGet(String URL) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60 * 1000);// 连接时间
		httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60 * 1000);// 数据传输时间
		HttpGet getRequest = new HttpGet(URL);
		HttpResponse response;
		try {
			response = httpClient.execute(getRequest);
			if (response.getStatusLine().getStatusCode() == 200) {
				String result = EntityUtils.toString(response.getEntity(), "UTF-8");
				httpClient.getConnectionManager().shutdown();
				return result.toString();
			} else {
				throw new Exception("请求外部系统出现异常");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ClientConnectionManager conn = httpClient.getConnectionManager();
			if (conn != null) {
				conn.shutdown();
			}
			httpClient = null;
		}
		return "";
	}

	// public static void main(String[] args) {
	// String url =
	// "https://mapi.alipay.com/gateway.do?service=notify_verify&partner=2088002396712354&notify_id=RqPnCoPT3K9%252Fvwbh3I%252BFioE227%252BPfNMl8jwyZqMIiXQWxhOCmQ5MQO%252FWd93rvCB%252BaiGg";
	//// String url = "http:///eyueportal/common/getIndustryList";
	// String retStr = ApacheHttpClient.httpGet(url);
	// System.out.println(retStr);
	// }
}
